/***************************************************************************************
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
*
* NEMU is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

/*#include <utils.h>*/
/*#include <device/map.h>*/
#include <common.h>

/*[> http://en.wikibooks.org/wiki/Serial_Programming/8250_UART_Programming <]*/
/*// NOTE: this is compatible to 16550*/

#define CH_OFFSET 0

static uint8_t *serial_base = NULL;


/*static void serial_putc(char ch) {*/
  /*MUXDEF(CONFIG_TARGET_AM, putch(ch), putc(ch, stderr));*/
/*}*/

static void serial_io_handler(uint32_t offset, int len, bool is_write) {
	if(!is_write)return;
	assert(len == 1);
	switch (offset) {
		/* We bind the serial port with the host stderr in NEMU. */
		case CH_OFFSET:
			printf("%c",serial_base[0]);
			/*putc(serial_base[0],stderr);*/
			break;
		default: panic("do not support offset = %d", offset);
	}
}

void init_serial() {
	serial_base = new_space(8);
	add_mmio_map("serial", SERIAL_ADDR, serial_base, 8, serial_io_handler);
}
